package com.hspedu.homework.Homework02;

import java.io.IOException;
import java.net.*;

/**
 * @author DL5O
 * @version 1.0
 */
public class SenderB {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(8887);

        //发送数据给ReceiveA
        byte[] question = "四大名著有哪些?".getBytes();
        DatagramPacket packet = new DatagramPacket(question, question.length, InetAddress.getByName("192.168.170.1"), 8888);
        System.out.println("发送数据...");
        socket.send(packet);
        System.out.println("发送数据完毕!");

        //接收从ReceiveA中发送的数据

        byte[] bytes = new byte[1024];
        DatagramPacket packet1 = new DatagramPacket(bytes, bytes.length);
        System.out.println("等待接收数据...");
        socket.receive(packet1);

        byte[] data = packet1.getData();
        int length = packet1.getLength();
        System.out.println(new String(data,0,length));
        System.out.println("数据接受完毕");
        System.out.println("SenderB退出");
        socket.close();
    }
}
